//
//  CellForViewControllerOfzhuanti.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CellForViewControllerOfzhuanti.h"
#import "CellTableViewForZhuanTi.h"
#import "NetworkingHandler.h"
#import "ModelOfZhuanTi.h"
#import "PCH.pch"
#import <UIImageView+WebCache.h>


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define HEADHEIGHT 64
#define FOOTHEIGHT 49

@interface CellForViewControllerOfzhuanti () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, retain)NSMutableArray *arrayForModel;
@property (nonatomic, retain) MBProgressHUD *hub;

@end

@implementation CellForViewControllerOfzhuanti

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_hub release];
    [_arrayForModel release];
    [_tableView release];
    [super dealloc];
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createTableView];
        
        [self handleData];
        

        
#pragma mark - 下拉刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self handleData];
  
            });
            
        }];
    }
    
    return self;
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    [self.tableView registerClass:[CellTableViewForZhuanTi class] forCellReuseIdentifier:@"pool"];
    
}

#pragma mark - 解析数据
- (void)handleData {
    
     self.arrayForModel = [NSMutableArray array];
    
    self.page = 1;
    NSString *string = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?page=%ld", (long)self.page];
    [NetworkingHandler networkingWithURL:string completeHandle:^(NSData *data,id result) {
        
        for (NSDictionary *dic in result) {
            
            ModelOfZhuanTi *model = [[ModelOfZhuanTi alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayForModel addObject:model];
            [model release];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
#pragma mark - 上拉加载
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            self.page = self.page + 1;
            NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?page=%ld", (long)self.page];
            
            [NetworkingHandler networkingWithURL:str completeHandle:^(NSData *data,id result) {
                
                for (NSDictionary *di in result) {
                    
                    ModelOfZhuanTi *model = [[ModelOfZhuanTi alloc] init];
                    [model setValuesForKeysWithDictionary:di];
                    [self.arrayForModel addObject:model];
                    [model release];
                    
                }
                [self.tableView reloadData];
                [self.hub hide:YES];
            }];
            
            [self.tableView.mj_footer endRefreshing];
            
        }];
    }];
}

#pragma mark - tableView协议方法
/** 返回行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

/** 返回行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayForModel.count;
}

/** 返回cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellTableViewForZhuanTi *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    [cell passModel:[self.arrayForModel objectAtIndex:indexPath.row]];
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    return cell;
}

#pragma mark - 布局子视图
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.tableView.frame = CGRectMake(0, 0, WIDTH, self.contentView.frame.size.height);

}

#pragma mark - 点击cell推出下一界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelOfZhuanTi *model = [self.arrayForModel objectAtIndex:indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passID1" object:nil userInfo:@{@"content" : indexPath, @"model" : model.id}];

}

- (void)viewWillAppear:(BOOL)animated {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    [self.tableView reloadData];
    
}












@end
